import { createWebHistory, createRouter } from 'vue-router'

import LoginPage from '@/pages/LoginPage.vue';
import LoginCard from '@/components/pageSpecific/LoginPageComponents/LoginCard.vue';
import ResetPasswdCard from '@/components/pageSpecific/LoginPageComponents/ResetPasswdCard.vue';
import PersonPage from '@/pages/PersonPage.vue';
import AdminPage from '@/pages/AdminPage.vue';
import CoursePage from '@/pages/CoursePage.vue';
import { useTokenStore } from '@/stores/token';

const routes = [
  {
    path: '/',
    component: LoginPage,
    children:[
      {
        path:'login',
        component: LoginCard,
        alias: 'login',
        meta:{
          title: '登录'
        },
        name: 'Login',
      },
      {
        path: 'forget',
        component: ResetPasswdCard,
        name:'ResetPasswd',
        meta:{
          title: '重置密码'
        }
      }
    ]
  },
  {
    path: '/admin',
    component: AdminPage,
    name: 'AdminPage',
    meta:{
      title: '管理员页面'
    }
  },
  {
    path: '/home',
    name: 'PersonPage',
    component: PersonPage,
    meta:{
      title: '主页'
    }
  },
  {
    path: '/course/:id',
    name: 'CoursePage',
    component: CoursePage,
    props: true,
    meta:{
      title: '课程页'
    }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes,
})

router.beforeEach(async (to) => {
  const token = useTokenStore();
  if (!(to.name === 'Login' || to.name === 'ResetPasswd') && !token.isLogin) {
    return { name: 'Login' };
  }
});

export default router;
